<template>
  <div>
    <el-container>
      <el-header height="100px">Quick-Html</el-header>
      <el-main>
        <el-table :data="tableData" style="width: 100%" empty-text="暂无关联站点">
          <el-table-column prop="name" label="名称" width="180"></el-table-column>
          <el-table-column prop="date" label="创建时间" width="260"></el-table-column>
          <el-table-column prop="id" label="HASH"></el-table-column>
          <el-table-column prop="path" label="路径"></el-table-column>

          <el-table-column label="操作" width="400">
            <el-row class="table-op" slot-scope="scope">
              <el-button type="primary" @click="openPanel(scope)" plain>控制面板</el-button>
              <!-- <el-button type="primary" @click="openSite(scope)" plain>打开站点</el-button> -->
              <el-button type="danger" @click="unlinkConfirm(scope)" plain>删除关联</el-button>
              <el-button
                type="success"
                @click="copyTemplate(scope)"
                icon="el-icon-circle-plus"
              >复制默认模板</el-button>
            </el-row>
          </el-table-column>
        </el-table>

        <el-row class="site-op">
          <el-button type="primary" @click="linkage" icon="el-icon-share">关联已有站点</el-button>
          <el-button type="warning" icon="el-icon-edit">修改默认模板</el-button>
          <el-button
            type="success"
            @click="dialogFormVisible = true"
            icon="el-icon-circle-plus"
          >本地新建站点</el-button>
        </el-row>

        <el-dialog title="本地新建站点" :visible.sync="dialogFormVisible" width="500px" ref="ruleForm">
          <el-form :model="form" :rules="rules">
            <el-form-item label="站点名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off" prop="name"></el-input>
            </el-form-item>
            <el-form-item label="站点路径" :label-width="formLabelWidth">
              <div style="text-align:left">{{homedir}}</div>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="createSite">确 定</el-button>
          </div>
        </el-dialog>

        <el-dialog title="删除关联站点" :visible.sync="removeDia" width="350px">
          <span>删除关联后将移除关联信息，但不会删除本地文件</span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="removeDia = false" type="primary" plain>取 消</el-button>
            <el-button type="danger" @click="unlink">确 定</el-button>
          </span>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<style>
  .el-header{
    line-height: 100px;
  }
.el-header,
.el-footer {
  background-color: #909399;
  text-align: left;
  color: #fff;
  font-size: 45px;
  font-weight: bold;
}

.site-op {
  margin-top: 60px;
}
</style>
<script>
import Vue from "vue";
import axios from "axios";
import { close, closeSync } from "fs";

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      removeDia: false,
      form: {
        name: "",
        type: [],
        desc: ""
      },
      activeId: "",
      rules: {
        name: { required: true, message: "请输入活动名称", trigger: "blur" }
      },
      formLabelWidth: "80px",
      homedir: ""
    };
  },
  mounted() {
    this.update();
  },
  methods: {
    afterSend(res) {
      this.$message({
        message: res.data.msg,
        type: res.data.status === true ? "success" : "warning"
      });
      if (res.data.status === true) {
        this.update();
      }
    },
    unlink() {
      this.removeDia = false;
      axios.post("/unlinksite", { id: this.activeId }).then(res => {
        this.afterSend(res);
      });
    },
    unlinkConfirm(target) {
      this.activeId = target.row.id;
      this.removeDia = true;
    },
    linkage() {
      console.log("关联操作");
    },
    createSite() {
      this.dialogFormVisible = false;
      axios
        .post("/create", {
          name: this.form.name
        })
        .then(res => {
          this.afterSend(res);
          this.form.name = "";
        });
    },
    copyTemplate(data) {
      axios.post("/copytemplate", { path: data.row.path }).then(res => {
        this.afterSend(res);
      });
    },
    update() {
      axios.get("/allprojects").then(res => {
        this.tableData = res.data.projects;
        this.homedir = res.data.homedir;
      });
    },
    openSite(data){
        axios.post("/opensite", { path: data.row.path }).then(res => {
        this.afterSend(res);
      });
    },
    openPanel(data){
    
     window.open('/sitepanel/'+data.row.id)
    }
  }
};
</script>